Python performance optimization